package com.liyuhan.onlinetaxibackground.Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

// 日期工具类，用来对日期进行操作
public class DateUtil {

    // 获取日期减去指定天数的日期
    public static Date subtractDays(Date date, int days) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(date);
        cal.add(Calendar.DATE, -days);

        return cal.getTime();
    }

    // 判断两个日期是否为同一天
    public static boolean isSameDay(Date date1, Date date2) {
        if (date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay(cal1, cal2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if (cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isBetween(Date date1, Date date2, Date date) {
        //判断某个日期是否在两个日期范围之内
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        date1 = TimeUtil.StringToDay(simpleDateFormat.format(date1));
        date2 = TimeUtil.StringToDay(simpleDateFormat.format(date2));
        date = TimeUtil.StringToDay(simpleDateFormat.format(date));

        if (date1.getTime() <= date.getTime() && date2.getTime() >= date.getTime()) {
            return true;
        }
        return false;
    }
}
